package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.entity.EventClickPageview;
import com.ddyx.entity.EventLog;
import com.ddyx.entity.WxUser;
import com.ddyx.mapper.EventClickPageviewMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.service.IEventClickPageviewService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MD5FileUtil;
import com.ddyx.util.MQUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class EventClickPageviewServiceImpl implements IEventClickPageviewService {

	@Resource
	EventClickPageviewMapper eventClickPageviewMapper;
	@Resource
	EventLogMapper eventLogMapper;
	@Resource

	@Autowired
	JmsTemplate jmsTemplate;

	@Autowired
	Destination topicDestination;

	/*
	 * 新增客户浏览首页接口 新增客户浏览商品详情页接口
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertPageview(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		String requestUrl = req.getRequestURL().toString();
		Date date = new Date();
		EventClickPageview eventClickPageview = new EventClickPageview();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(eventClickPageview, paramMap);
			eventClickPageview.setEventTime(date);
			eventClickPageviewMapper.insert(eventClickPageview);

			// 新增门店客户
			paramMap.put("actionType", EventTag.NEWCUSTOMER);
			paramMap.put("createTime", date);
			MQUtil.sendObMsg(paramMap, jmsTemplate, topicDestination);

			// {// 用户事件信息同步
			// if (eventClickPageview.getGoodsId() != null) {// 如果商品id不为空,则该访问为 浏览商品详情页
			// EventLog eventLog = new EventLog();
			// eventLog.setWxUid(eventClickPageview.getWxUid());
			// eventLog.setEventTag(EventTag.BROWSEGOODSDETAILS);
			// eventLog.setEventTime(eventClickPageview.getEventTime());
			// eventLog.setStoreNo(eventClickPageview.getStoreNo());
			// eventLog.setRequestUrl(requestUrl);
			// // String urlMd5 = MD5FileUtil.getMD5String(requestUrl);
			// eventLog.setUrlMd5(MD5FileUtil.getMD5String(requestUrl));
			// eventLogMapper.insertSelective(eventLog);
			// }
			// }

		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}

		return result;
	}

	/*
	 * 获取所有浏览该门店商品的客户
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getBrowseCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		// EventClickPageview eventClickPageview = new EventClickPageview();
		try {
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<WxUser> list = eventClickPageviewMapper.getBrowseCustomer(paramMap);
			result.setData(list);
			result.setTotal((int) pageHelper.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	/*
	 * 用户获取全部浏览过的记录(弃用) 商品信息 预约状态 预约总人数
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getCustomerAllPV(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<Map<String, Object>> list = eventClickPageviewMapper.getCustomerAllPV(paramMap);
			result.setData(list);
			result.setTotal((int) pageHelper.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getBrowsedGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			Map paramMap = (Map) param.getData();
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			try {
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List goodsList = eventClickPageviewMapper.getBrowsedGoods(paramMap);
				result.setData(goodsList);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public ResponseForm getBrowsedStore(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			Map paramMap = (Map) param.getData();
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			try {
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List storeList = eventClickPageviewMapper.getBrowsedStore(paramMap);
				result.setData(storeList);
				result.setTotal((int) pageHelper.getTotal());
			} catch (Exception e) {
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
				e.printStackTrace();
			}
		}
		return result;
	}

}
